package com.whcoding.jpa.utils;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;

/**
 * @program: spring-boot-learning
 * @description:
 * @author: whcoding
 * @create: 2022-09-06 16:22
 **/
public class JpaPageUtil {

	/**
	 * Jpa需要的分页对象
	 *
	 * @param page
	 * @return
	 */
	public static PageRequest buildPageable(PageParamVO page) {
		Sort sort = Sort.unsorted();
		if (StringUtils.isEmpty(page.getSortIndx())) {
			page.setSortIndx("modifiedTime");
		}
		if (StringUtils.isEmpty(page.getSortDir())) {
			page.setSortDir(Sort.Direction.DESC.name());
		}
		sort = new Sort(Sort.Direction.fromString(page.getSortDir()), page.getSortIndx());
		if (page.getPage() == null||page.getPage() <= 0) {
			page.setPage(1);
		}
		if (page.getLimit() == null) {
			page.setLimit(10000);
		}
		PageRequest pageRequest = PageRequest.of(page.getPage() - 1, page.getLimit(), sort);
		return pageRequest;
	}
}
